<template>
  <el-dialog :title="!dataForm.liveId ? '新增' : '修改'"
             :close-on-click-modal="false"
             :visible.sync="visible">
    <el-form :model="dataForm"
             :rules="dataRule"
             ref="dataForm"
             @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="直播名"
                    prop="liveAme">
        <el-input v-model="dataForm.liveAme"
                  placeholder="直播名"></el-input>
      </el-form-item>
      <el-form-item label="开始时间"
                    prop="startDate">
        <el-date-picker v-model="dataForm.startDate"
                        type="datetime"
                        placeholder="选择日期时间"
                        value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间"
                    prop="endDate">
        <el-date-picker v-model="dataForm.endDate"
                        type="datetime"
                        placeholder="选择日期时间"
                        value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="商品"
                    prop="goodsLive">
        <el-form :inline="true"
                 :model="goods"
                 @keyup.enter.native="getDataList()">
          <el-form-item>
            <el-input v-model="goods.key"
                      placeholder="参数名"
                      clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="getDataList()">查询</el-button>
          </el-form-item>
        </el-form>
        <el-table :data="dataList"
                  border
                  v-loading="dataListLoading"
                  @selection-change="selectionChangeHandle"
                  style="width: 100%;">
          <el-table-column type="selection"
                           header-align="center"
                           align="center"
                           width="50">
          </el-table-column>
          <el-table-column prop="goodsTitle"
                           header-align="center"
                           align="center"
                           label="商品标题">
          </el-table-column>
          <el-table-column prop="goodsPrice"
                           header-align="center"
                           align="center"
                           label="商品单价">
            <template slot-scope="scope">
              <el-input v-model="scope.row.goodsPrice"
                        placeholder="请输入内容"></el-input>
            </template>
          </el-table-column>
          <el-table-column prop="goodsStock"
                           header-align="center"
                           align="center"
                           label="商品库存，-1表示没有限制">
            <template slot-scope="scope">
              <el-input v-model="scope.row.goodsStock"
                        placeholder="请输入内容"></el-input>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination @size-change="sizeChangeHandle"
                       @current-change="currentChangeHandle"
                       :current-page="pageIndex"
                       :page-sizes="[10, 20, 50, 100]"
                       :page-size="pageSize"
                       :total="totalPage"
                       layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </el-form-item>
    </el-form>
    <span slot="footer"
          class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary"
                 @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      goods: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      selectionGood: [],
      addOrUpdateVisible: false,
      visible: false,
      dataForm: {
        liveId: 0,
        liveAme: '',
        startDate: '',
        endDate: ''
      },
      dataRule: {
        liveAme: [
          { required: true, message: '直播名不能为空', trigger: 'blur' }
        ],
        startDate: [
          { required: true, message: '开始时间不能为空', trigger: 'blur' }
        ],
        endDate: [
          { required: true, message: '结束时间不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/goods/goods/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.goods.key
        })
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      console.log(val)
      this.dataListSelections = val
    },
    init (id) {
      this.dataForm.liveId = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.liveId) {
          this.$http({
            url: this.$http.adornUrl(`/live/livestream/info/${this.dataForm.liveId}`),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.liveAme = data.liveStream.liveAme
              this.dataForm.startDate = data.liveStream.startDate
              this.dataForm.endDate = data.liveStream.endDate
            }
          })
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.selectionGood = []
          for (let index = 0; index < this.dataListSelections.length; index++) {
            this.selectionGood.push({
              goodsId: this.dataListSelections[index].id,
              stockCount: this.dataListSelections[index].goodsStock,
              price: this.dataListSelections[index].goodsPrice
            })
          }
          console.log(this.selectionGood)
          this.$http({
            url: this.$http.adornUrl(`/live/livestream/${!this.dataForm.liveId ? 'save' : 'update'}`),
            method: 'post',
            data: this.$http.adornData({
              'liveId': this.dataForm.liveId || undefined,
              'liveAme': this.dataForm.liveAme,
              'startDate': this.dataForm.startDate,
              'endDate': this.dataForm.endDate,
              'selectionGood': this.selectionGood
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
